package com.wtwd.device.service.impl;


import com.wtwd.device.constant.Constant;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.FriendRequestsMapper;
import com.wtwd.device.model.entity.AppUser;
import com.wtwd.device.model.entity.FriendRequests;
import com.wtwd.device.model.entity.FriendRequestsVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.AppUserInfoService;
import com.wtwd.device.service.AppUserService;
import com.wtwd.device.service.FriendRequestsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @since 2021-09-16 10:37:28
 */
@Service
public class FriendRequestsServiceImpl implements FriendRequestsService {
    @Resource
    private FriendRequestsMapper friendRequestsMapper;

    @Resource
    private AppUserInfoService appUserInfoService;

    @Resource
    private AppUserService appUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FriendRequests queryById(Long id) {
        return this.friendRequestsMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param friendRequests 筛选条件
     * @param pageRequest    分页对象
     * @return 查询结果
     */
    @Override
    public Page<FriendRequests> queryByPage(FriendRequests friendRequests, PageRequest pageRequest) {
        long total = this.friendRequestsMapper.count(friendRequests);
        return new PageImpl<>(this.friendRequestsMapper.queryAllByLimit(friendRequests, pageRequest), pageRequest, total);
    }

    /**
     * 添加好友
     *
     * @param friendRequests 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insert(FriendRequests friendRequests) {
        //添加申请好友记录，状态为申请中
        friendRequests.setState(1);
        //记录申请时间
        friendRequests.setApplyTime(new Date().getTime());
        int rest = friendRequestsMapper.insert(friendRequests);
        return rest;
    }

    /**
     * 修改数据
     *
     * @param friendRequests 实例对象
     * @return 实例对象
     */
    @Override
    public FriendRequests update(FriendRequests friendRequests) {
        friendRequestsMapper.update(friendRequests);
        return this.queryById(friendRequests.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return friendRequestsMapper.deleteById(id) > 0;
    }

    /**
     * 修改申请好友表记录状态
     *
     * @param type
     * @param id
     * @return
     */

    @Override
    public Integer updateFriendRequestStatus(Integer type, Long id, Long recoveryTime) {
        return friendRequestsMapper.updateFriendRequestStatus(type, id, recoveryTime);
    }

    @Override
    public RespEntity getFriendRequestsList(Integer currentPage, Integer pageSize, Long userId) {
        Integer start = pageSize * currentPage;
        //查询用户信息数据(根据已知用户表，用户id,查询其他条件,避免if else构建)
        AppUser user = appUserService.getUserByUserId(userId);
        if (user == null) {
            return new RespEntity<>(RespCode.USER_EXCEPTION);
        }
        List<FriendRequestsVo> friendRequestsVos = friendRequestsMapper.getFriendRequestsInfoList(start, pageSize,userId, Constant.FRIEND_STATE_APPLY);
        if (friendRequestsVos!=null&&friendRequestsVos.size()>0){
            for (FriendRequestsVo friendRequestsVo : friendRequestsVos) {
                if (StringUtils.isBlank(friendRequestsVo.getName())){
                    if (StringUtils.isNotBlank(user.getPhone())){
                        friendRequestsVo.setName(user.getPhone());
                    }else if (StringUtils.isNotBlank(user.getEmail())){
                        friendRequestsVo.setName(user.getEmail());
                    }else {
                        friendRequestsVo.setName(Constant.DEFAULT_USER_NAME);
                    }
                }
            }
        }
        return new RespEntity(RespCode.SUCCESS,friendRequestsVos);
    }

    @Override
    public List<FriendRequests> getFriendRequestByConditions(FriendRequests selectFriend) {
        return friendRequestsMapper.getFriendRequestByConditions(selectFriend);
    }

    @Override
    public RespEntity getUserRequestsList(Integer currentPage, Integer pageSize, Long userId) {
        Integer start = pageSize * currentPage;
        //查询用户信息数据(根据已知用户表，用户id,查询其他条件,避免if else构建)
        AppUser user = appUserService.getUserByUserId(userId);
        if (user == null) {
            return new RespEntity<>(RespCode.USER_EXCEPTION);
        }
        List<FriendRequestsVo> friendRequestsVos = friendRequestsMapper.getUserRequestsInfoList(start, pageSize,userId,Constant.FRIEND_STATE_AGREE);
        if (friendRequestsVos!=null&&friendRequestsVos.size()>0){
            for (FriendRequestsVo friendRequestsVo : friendRequestsVos) {
                if (StringUtils.isBlank(friendRequestsVo.getName())){
                    if (StringUtils.isNotBlank(user.getPhone())){
                        friendRequestsVo.setName(user.getPhone());
                    }else if (StringUtils.isNotBlank(user.getEmail())){
                        friendRequestsVo.setName(user.getEmail());
                    }else {
                        friendRequestsVo.setName(Constant.DEFAULT_USER_NAME);
                    }
                }
            }
        }
        return new RespEntity(RespCode.SUCCESS,friendRequestsVos);
    }

    @Override
    public FriendRequests getApplyFriendRequestByProposerAndRespondent(Long proposer, Long respondent) {
        return friendRequestsMapper.getApplyFriendRequestByProposerAndRespondent(proposer, respondent);
    }

    @Override
    public RespEntity getFriendAllApplyById(String userId, Integer pageSize, Integer currentPage) {
        Integer start = pageSize * currentPage;
        List<FriendRequests> friendAllApplyById = friendRequestsMapper.getFriendAllApplyById(userId, start, pageSize);
        return new RespEntity(RespCode.SUCCESS,friendAllApplyById);
    }

}
